/**
 * 房间管理
 */
import { Op } from 'sequelize'

class AdminBuildingRoomController extends BasicController {

    /**
     * 房间列表
     * @return {[type]} [description]
     */
    async actionColumn() {
        let { search, region_id, building_id } = $_POST;
        let where = {};
        if (search) {
            where['name'] = {
                [Op.like]: '%' + search + '%'
            }
        }
        if (region_id) {
            where['region_id'] = region_id;
        }
        if (building_id) {
            where['building_id'] = building_id;
        }

        return await this.modules('Building.Room').where(where).fields(['id', 'name', 'floor_num', 'building_id', 'region_id', 'company_id']).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 房间列表
     * @return {[type]} [description]
     */
    async actionIndex() {
        let { page, search, region_id, building_id } = $_POST;
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {};
        if (search) {
            where['name'] = {
                [Op.like]: '%' + search + '%'
            }
        }
        if (region_id) {
            where['region_id'] = region_id;
        }
        if (building_id) {
            where['building_id'] = building_id;
        }

        return await this.modules('Building.Room').where(where).joinWith(['region', 'building', 'company']).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 房间详情
     * @return {[type]} [description]
     */
    async actionView() {
        let { id } = $_POST;
        let data = await this.modules('Building.Room').where({ id }).joinWith(['region', 'building', 'company']).first();
        if (data) {
            return data;
        } else {
            throw new Error('数据不存在');
        }
    }

    /**
     * 新增房间
     * @return {[type]} [description]
     */
    async actionCreate() {
        let { building_id, name } = $_POST;
        let check = await this.modules('Building.Room').where({ building_id, name }).first();
        if (check) {
            throw new Error('房间号已存在');
        }
        let data = await this.modules('Building.Room').insert($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }
    /**
     * 
     * 新增房间(批量)
     * @returns 
     */
    async actionBlukCreate() {
        let{ building_id }=$_POST;
        let { rooms } = $_POST;
        let names=rooms.map(p=>p.name);
        console.log('room',rooms)
        let check = await this.modules('Building.Room').where({ building_id, name:names }).first();
        if (check) {
            throw new Error(`房间号${check.name}已存在`);
        }
        let data = await this.modules('Building.Room').bulkCreate(rooms);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 编辑房间
     * @return {[type]} [description]
     */
    async actionUpdate() {
        let { id, building_id, name } = $_POST;
        let check = await this.modules('Building.Room').where({ building_id , name }).first();
        if (check && check.id != id) {
            throw new Error('房间号不能重复');
        }
        let data = await this.modules('Building.Room').where({ id }).update($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 删除房间
     * @return {[type]} [description]
     */
    async actionDelete() {
        let { id } = $_POST;
        let res = await this.modules('Building.Room').where({ id }).delete();
        if (res) {
            return true;
        } else {
            throw new Error('删除失败');
        }
    }

}

export default AdminBuildingRoomController;